<?php
require_once dirname(__DIR__) .  '/plugins/poster/PosterMaker.php';
require_once dirname(__DIR__) .  '/plugins/poster/QRcode.php';
class Posterplugs
{

    //分享商品图
    public function share_goods_poster($h_params){
        $goods_title = $h_params['title'];
        if(strlen($goods_title)>60){
            $goods_title = substr($goods_title,0,60).'...';
        }
        $h_params['monthsale']=$h_params['monthsale'].'人购买';
        $h_params['price']='原价'.$h_params['price'];
        $h_params['couponmoney']=$h_params['couponmoney'].'元';
        $poster = new Postermaker(750, 1340);
        $poster->addImg($h_params['img_url'], [0,0], [750,750])
            ->addImg('http://qn.enshitc.com/logo_t.png', [30,30],[100, 50])
            ->addImg($h_params['site_icon'], [20,790],[35,35])          //平台图标
            ->addText($goods_title, 24, [60, 820], [0, 0, 0])                           //商品名称
            ->addText($h_params['monthsale'], 24, [20, 890], [101,101,101])
            ->addText('劵后￥', 20, [20, 950], [255, 0, 0])
            ->addText($h_params['endprice'], 30, [100, 950], [255, 0, 0])
            ->addText($h_params['price'], 20, [220, 950], [101,101,101])
            ->addText('------------', 20, [205, 950], [101,101,101])
            ->addImg('http://qn.enshitc.com/juan.png', [30,990],[200, 70])          //劵图标
            ->addText('劵', 30, [45,1040], [255,255,255])
            ->addText($h_params['couponmoney'], 25, [130, 1038], [255,255,255])
            ->addQrCode($h_params['taoword'], [500,850],[250,250])                              //二维码
            ->addText('长按扫码领劵', 20, [540,1120], [101,101,101])
            ->addImg('http://qn.enshitc.com/juan_detail.png', [0,1150],[750, 200]);          //劵图标
            //->render();
        $filepath = BASE_PATH.'public/haibao/p.jpg';          //生成的图片
        $poster_img = $poster->banse64_render($filepath);
        return $poster_img;
    }




    //邀请海报
    public function invite_poster($bg_img,$down_url,$invite_code){
        $filepath = BASE_PATH.'public/haibao/p.jpg';          //生成的图片
        $invite_code = "邀请码：".$invite_code;
        $poster = new Postermaker(1080, 1920);
        $poster
            ->addImg($bg_img, [0,0], [1080, 1920])
            ->addImg('http://qn.enshitc.com/logo_t.png', [30,30],[100, 40])
            ->addQrCode($down_url, [450,1520],[250,250])
            ->addText($invite_code, 28, [440,1815], [0, 0, 0]);
        $poster_img = $poster->banse64_render($filepath);
        return $poster_img;

    }










    //测试海报生成类
    public function test()
    {
        $share_img = 'https://img.alicdn.com/imgextra/i3/3012913363/O1CN01A330Tg1aiIXDclngi_!!3012913363.jpg';
        //计算原图长宽比例
        //新图尺寸
        //生成海报
        $font = APP_PATH.'public/static/simsun.ttf';   //保证字体文件目录正确
        $font_cu = APP_PATH.'public/static/songcu.ttf';   //保证字体文件目录正确
        $filepath = APP_PATH.'public/static/haibao/p.jpg';
        $posters_width = 450;                   //画布宽度
        $posters_height = 700;
        $newimg = imagecreatetruecolor($posters_width, $posters_height);
        $bg = imagecolorallocate($newimg, 244, 244, 244);
        imagefill($newimg,0,0,$bg);
        //写分享图片
        $new_shareim = imagecreatefromstring(file_get_contents($share_img));
        imagecopy($newimg,$new_shareim,0,0,0,0,450,350);      //写入商品图片

        //-----------------------------------------------------------商品相关----------------------------------
        $filepath_goods = APP_PATH.'public/static/haibao/goods_p.jpg';
        $posters_width = 450;                   //画布宽度
        $posters_height = 200;
        $goods_newimg = imagecreatetruecolor($posters_width, $posters_height);
        $goods_bg = imagecolorallocate($goods_newimg, 255, 255, 255);
        imagefill($goods_newimg,0,0,$goods_bg);
        //商品标题
        $black = imagecolorallocate($goods_newimg, 0, 0, 0);
        imagettftext($goods_newimg,15,0,30,40,$black,$font_cu,'这里是商品标题');
        $black = imagecolorallocate($goods_newimg, 176, 176, 176);
        imagettftext($goods_newimg,12,0,30,80,$black,$font,'199人已购买');
        $black = imagecolorallocate($goods_newimg, 255, 0, 0);
        imagettftext($goods_newimg,13,0,30,120,$black,$font,'券后￥');
        imagettftext($goods_newimg,20,0,100,120,$black,$font_cu,'49.9');
        $black = imagecolorallocate($goods_newimg, 176, 176, 176);
        imagettftext($goods_newimg,12,0,170,120,$black,$font,'原价777.9');
        imagettftext($goods_newimg,12,0,170,120,$black,$font,'----------');
        $share_img = 'http://jzcloud.jiruan.ltd/font/juan.png';
        $new_shareim = imagecreatefromstring(file_get_contents($share_img));
        imagecopy($goods_newimg,$new_shareim,30,150,0,0,99,30);      //写入商品图片
        $black = imagecolorallocate($goods_newimg, 255, 255, 255);
        imagettftext($goods_newimg,15,0,30,172,$black,$font_cu,' 卷 30元');
        //写二维码
        require_once "phpqrcode.php";
        $errorCorrectionLevel = 'L';  //容错级别
        $matrixPointSize = 6;      //生成图片大小
        //生成二维码图片
        ob_start();
        QRcode::png('nihao',false,$errorCorrectionLevel, $matrixPointSize, 2);
        //$imgstr = 'data:image/png;base64,'.base64_encode(ob_get_contents());
        $imgstr =base64_encode(ob_get_contents());
        ob_end_clean();
        if (strstr($imgstr,",")){
            $imgstr = explode(',',$imgstr);
            $imgstr = $imgstr[1];
        }
        $imageName = APP_PATH.'public/static/haibao/'.date("His",time())."_".rand(1111,9999).'.jpg';
        $ostream_image = file_put_contents($imageName, base64_decode($imgstr));//返回的是字节数
        if($ostream_image) {
            //$er_shareim = imagecreatefromstring(file_get_contents('http://jzcloud.jiruan.ltd/img/ad/tb.png'));
            $er_shareim = imagecreatefromstring(file_get_contents($imageName));
            imagecopy($goods_newimg, $er_shareim, 260, 15, 0, 0, 150, 150);
            $gray = imagecolorallocate($goods_newimg, 136, 136, 136);
            @unlink($imageName);
        }
        $black = imagecolorallocate($goods_newimg, 144, 144, 144);
        imagettftext($goods_newimg,12,0,275,175,$black,$font,'长按二维码领劵');

        //海报生成保存
        imagejpeg($goods_newimg, $filepath_goods);
        ImageDestroy($goods_newimg);
        $new_shareim = imagecreatefromstring(file_get_contents($filepath_goods));
        imagecopy($newimg,$new_shareim,10,370,0,0,430,200);      //写入商品图片


        $changan = APP_PATH."public/static/haibao/changan.jpg";//http://jzcloud.jiruan.ltd/font/changan.jpg
        $new_shareim = imagecreatefromstring(file_get_contents($changan));
        imagecopy($newimg,$new_shareim,0,560,0,0,450,133);      //写入商品图片
        //海报生成保存
        imagejpeg($newimg, $filepath);
        ImageDestroy($newimg);

        $base64_file = '';
        if(file_exists($filepath)){
            $mime_type= mime_content_type($filepath);//mime_content_type
            $base64_data = base64_encode(file_get_contents($filepath));
            $base64_file = 'data:'.$mime_type.';base64,'.$base64_data;
        }
        $this->ok($base64_file);
    }

}